define(function(require,exports,module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	var urlTool = require("resource/js/component/urlTool.js");
	var ajax = require("resource/js/component/ajax.js");
	var clickNode = function (processKey,businessName){
		var urlPath = BASEPATH+'/oa/act/activitiMain/getProcessPage.htm?key='+processKey+'&businessName='+urlTool.ToUnicode(businessName);
		var backDlg = require("resource/js/component/backDialog");
		backDlg.openBackDialog({
			href:urlPath,
			title:'新建审批-'+businessName
		});
	};
	var initPage=function(){
		ajax.init().success(function(code,msg,data){
			var htmlList="";
			if(code==0){
				if(data.length>0){
					for(var k in data){
						$('<div id='+data[k].groupId+' class="newApproval"><div class="listTitle"><span></span>'+data[k].groupName+'</div>'+
								'<div class="listTask"><ul class="clear" style="width:100%;"></ul></div></div>').appendTo($(".newApprovalList"));
						var roundNumber = parseInt(Number($(window).width())/183);
						$.each(data[k].processModel,function(i,view){
							if(i>roundNumber-2){
								$('<li class="current nextEight"><img style="position: relative;top:20px;" src="'+BASEPATH+view.iconUrl+'" width="46" />'+
								  '<a href="javascript:void(0)" id='+view.businessId+'>'+view.businessName+'</a></li>').appendTo($("#"+data[k].groupId).find("ul")).each(function(){
									   $.data(this,{data:view});
									});
							}else{
								$('<li><img style="position: relative;top:20px;" src="'+BASEPATH+view.iconUrl+'" width="46" />'+
								  '<a href="javascript:void(0)" id='+view.businessId+'>'+view.businessName+'</a></li>').appendTo($("#"+data[k].groupId).find("ul")).each(function(){
										$.data(this,{data:view});
									});
							}
						});

						if($("#"+data[k].groupId).find("li").length>roundNumber-1){
							$('<li class="listMore active">展开更多</li>').appendTo($("#"+data[k].groupId).find("ul")).unbind("click").on("click",function(){
								var $this=$(this);
								if($this.hasClass("active")){
									$this.siblings("li").removeClass("current");
									$this.removeClass("active");
									$this.text("收起");
								}else{
									$this.siblings("li.nextEight").addClass("current");
									$this.addClass("active");
									$this.text("展开更多");
								}
							});
						}
					}

					$(".listTitle").on("click",function(){
						if($(this).hasClass("titleActive")){
							$(this).removeClass("titleActive");
						}else{
							$(this).addClass("titleActive");
						}
						$(this).siblings("div.listTask").slideToggle();
					});

					$("#listTask li").unbind("click");
					$(".listTask li").on("click",function(){
						var viewData = $.data(this).data;
						if(viewData!=null&&viewData.hasOwnProperty("processKey")&&viewData.hasOwnProperty("businessName")){
							clickNode(viewData.processKey,viewData.businessName);
						}
					});

					$("#noDatamsg").hide();
					$("#newApprovalList").show();

				}else{
					$("#noDatamsg").find('p').text('您的企业还没添加可用的审批');
					$("#noDatamsg").show();
					$("#newApprovalList").hide();
				}
			}
		}).error(function(code,msg,datas){
			$(".error").text("请求失败！请检查网络连接，稍后再试").show().delay(2000).hide(0);
		}).post(BASEPATH+"/oa/act/processGroup/queryGroupsAndProcess.htm",{status:'0',processStatus:'0'});
	};
	module.exports={
		trans:function(){
			initPage();
		}
	}
});

